//
//  InvesmentProductNameViewController.m
//  SP2P
//
//  Created by Eternal on 2017/8/23.
//  Copyright © 2017年 EIMS. All rights reserved.
//

#import "InvesmentProductNameViewController.h"
#import "InvestmentDetailViewController.h"
#define kButtonNum 3
@interface InvesmentProductNameViewController ()<UIScrollViewDelegate>
{
    UILabel *_earningsLabel;
    UILabel *_productTimeLabel;
    UILabel *_productSizeLabel;
    UIButton *_productDetailButton;
    UIButton *_propertyDirectionButton;
    UIButton *_securityWayButton;
    UIView *_buttonView;
    UIView *_buttonLineView;

}
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *productInstruLabel;
@property (weak, nonatomic) IBOutlet UIButton *buyProductBtn;

@property (weak, nonatomic) IBOutlet UIScrollView *contentScroView;
@property (weak, nonatomic) IBOutlet UIScrollView *titleScrollView;
@property (nonatomic, strong) UIScrollView *detailScrollView;
@end

@implementation InvesmentProductNameViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
    
}
#pragma mark - initView
- (void)initView{
    [self initNavBarTitle:@"项目详情" andLeftItemName:@"返回" andRightItemName:@""];
    self.titleScrollView.delegate = self;
    self.titleLabel.font = KFontSize(13);
    self.buyProductBtn.titleLabel.font = KFontSize(12);
    self.productInstruLabel.font = KFontSize(10);
    self.buyProductBtn.layer.cornerRadius = 6;
    self.buyProductBtn.layer.masksToBounds = YES;
    UIView *bottomView = [UIView viewWithFrame:RECTMAKE(0, GetMaxY(self.titleLabel), MSWIDTH, KAdaptNum(60)) backgroundColor:[UIColor whiteColor] superview:self.contentScroView];
    
    NSArray *titleArr = @[@"尊享年化收益",@"项目期限",@"项目规模"];
        for (NSInteger i=0; i < titleArr.count; i++) {
        UILabel *value = [UILabel labelWithFrame:RECTMAKE(MSWIDTH/3*i + 5, KAdaptNum(30), MSWIDTH/3 - 10, KAdaptNum(20)) text:@"" textColor:KBlackColor textFont:11 fitWidth:YES superview:bottomView];
        value.textAlignment = NSTextAlignmentCenter;
        if (i == 0) {
            _earningsLabel = value;
        }else if (i == 1) {
            _productTimeLabel = value;
        }else if (i == 2) {
            _productSizeLabel = value;
        }
        _earningsLabel.text = @"9.0%";
        _earningsLabel.font = KFontSize(20);
        _earningsLabel.textColor = KColor;
        _productTimeLabel.attributedText = [MyTools getNSMutableAttributedStringWithText:@"12个月" start:0 end:2 textColor:KColor textFont:KFontSize(20)];
        _productSizeLabel.attributedText = [MyTools getNSMutableAttributedStringWithText:@"5000万" start:0 end:4 textColor:KColor textFont:KFontSize(20)];
        UILabel *label = [UILabel labelWithFrame:RECTMAKE(GetMinX(value), GetMaxY(value), GetWidth(value), KAdaptNum(20)) text:titleArr[i] textColor:KColor textFont:11 fitWidth:YES superview:bottomView];
        label.textAlignment = NSTextAlignmentCenter;
        
    }

    [self loadDetailScrollView];
}
- (void)loadDetailScrollView {
    CGFloat scrollOriginY = KAdaptNum(45);
    
    _buttonView = [UIView viewWithFrame:CGRectMake(0, 0, MSWIDTH, scrollOriginY) backgroundColor:[UIColor whiteColor] superview:self.titleScrollView];
    
    _productDetailButton = [UIButton buttonWithFrame:CGRectMake(0, 0, MSWIDTH/3, scrollOriginY - 1) backgroundColor:nil title:@"项目描述" titleColor:KColor titleFont:14.0 target:self action:@selector(headerButtonClick:) superview:_buttonView];
    
    _propertyDirectionButton = [UIButton buttonWithFrame:CGRectMake(MSWIDTH/3, 0, MSWIDTH/3, GetHeight(_productDetailButton)) backgroundColor:nil title:@"保障措施" titleColor:KBlackColor titleFont:14.0 target:self action:@selector(headerButtonClick:) superview:_buttonView];
    
    _securityWayButton = [UIButton buttonWithFrame:CGRectMake(MSWIDTH/3*2, 0, MSWIDTH/3, GetHeight(_productDetailButton)) backgroundColor:nil title:@"项目参数" titleColor:KBlackColor titleFont:14.0 target:self action:@selector(headerButtonClick:) superview:_buttonView];
    
    UIView *buttonLine = [UIView viewWithFrame:CGRectMake(0, CGRectGetMaxY(_productDetailButton.frame), MSWIDTH, _buttonView.frame.size.height - CGRectGetMaxY(_productDetailButton.frame)) backgroundColor:KlayerBorder superview:_buttonView];
    
    _buttonLineView = [UIView viewWithFrame:CGRectMake(0, 0, MSWIDTH/3, buttonLine.frame.size.height) backgroundColor:KColor superview:buttonLine];
    _detailScrollView = [UIScrollView scrollViewWithFrame:RECTMAKE(0, GetMaxY(_buttonView), MSWIDTH, self.titleScrollView.frame.size.height - GetHeight(_buttonView)) delegate:self backgroundColor:KbackgroundColor superview:self.titleScrollView];
    _detailScrollView.contentSize = CGSizeMake(MSWIDTH * 3,0);
    
    _detailScrollView.pagingEnabled = YES;
    for (int i=0; i<3; i++) {
        UIView *view = [[UIView alloc] initWithFrame:RECTMAKE(MSWIDTH*i, 0,MSWIDTH,_detailScrollView.frame.size.height)];
        if (i==0) {
            view.backgroundColor = [UIColor whiteColor];
        }else if(i == 1){
           view.backgroundColor = [UIColor redColor];
        }else{
         view.backgroundColor = [UIColor blueColor];
        }
        [_detailScrollView addSubview:view];
        
    }
    
    self.contentScroView.contentSize = CGSizeMake(0,MSHIGHT+50);
}

#pragma mark - Action
- (IBAction)clickShopProduct:(id)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)headerButtonClick:(UIButton *)button {
    if (button == _productDetailButton) {
        [UIView animateWithDuration:0.3 animations:^{
            CGRect frame = _buttonLineView.frame;
            frame.origin.x = 0;
            _buttonLineView.frame = frame;
            _detailScrollView.contentOffset = CGPointMake(0, 0);
        } completion:^(BOOL finished) {
            [self buttonChange:button];
        }];
    }else if(button == _propertyDirectionButton){
        [UIView animateWithDuration:0.3 animations:^{
            CGRect frame = _buttonLineView.frame;
            frame.origin.x = MSWIDTH/kButtonNum;
            _buttonLineView.frame = frame;
            _detailScrollView.contentOffset = CGPointMake(MSWIDTH, 0);
        } completion:^(BOOL finished) {
            [self buttonChange:button];
        }];
    }else {
        [UIView animateWithDuration:0.3 animations:^{
            CGRect frame = _buttonLineView.frame;
            frame.origin.x = (MSWIDTH/kButtonNum)*2;
            _buttonLineView.frame = frame;
            _detailScrollView.contentOffset = CGPointMake(MSWIDTH*2, 0);
        } completion:^(BOOL finished) {
            [self buttonChange:button];
        }];
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    if (scrollView == _detailScrollView) {
        double OriginX = scrollView.contentOffset.x;
        if (OriginX>=0 && OriginX<=MSWIDTH*2) {
            CGRect frame = _buttonLineView.frame;
            frame.origin.x = (OriginX/MSWIDTH)*(MSWIDTH/kButtonNum);
            _buttonLineView.frame = frame;
            
            if (OriginX <= MSWIDTH/2) {
                [self buttonChange:_productDetailButton];
            }else if (OriginX>MSWIDTH/2 && OriginX<MSWIDTH+MSWIDTH/2) {
                [self buttonChange:_propertyDirectionButton];
            }else {
                [self buttonChange:_securityWayButton];
            }
        }
        
    }
}

- (void)buttonChange:(UIButton *)button {
    if (button == _productDetailButton) {
        [_productDetailButton setTitleColor:KColor forState:UIControlStateNormal];
        _productDetailButton.titleLabel.font = KFontBoldSize(14.0);
        
        [_propertyDirectionButton setTitleColor:KBlackColor forState:UIControlStateNormal];
        _propertyDirectionButton.titleLabel.font = KFontSize(14.0);
        [_securityWayButton setTitleColor:KBlackColor forState:UIControlStateNormal];
        _securityWayButton.titleLabel.font = KFontSize(14.0);
    }else if (button == _propertyDirectionButton) {
        [_productDetailButton setTitleColor:KBlackColor forState:UIControlStateNormal];
        _productDetailButton.titleLabel.font = KFontSize(14.0);
        
        [_propertyDirectionButton setTitleColor:KColor forState:UIControlStateNormal];
        _propertyDirectionButton.titleLabel.font = KFontBoldSize(14.0);
        [_securityWayButton setTitleColor:KBlackColor forState:UIControlStateNormal];
        _securityWayButton.titleLabel.font = KFontSize(14.0);
    }else {
        [_productDetailButton setTitleColor:KBlackColor forState:UIControlStateNormal];
        _productDetailButton.titleLabel.font = KFontSize(14.0);
        
        [_propertyDirectionButton setTitleColor:KBlackColor forState:UIControlStateNormal];
        _propertyDirectionButton.titleLabel.font = KFontSize(14.0);
        [_securityWayButton setTitleColor:KColor forState:UIControlStateNormal];
        _securityWayButton.titleLabel.font = KFontBoldSize(14.0);
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
